package com.xuecheng.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuecheng.teaching.mapper.WorkMapper;
import com.xuecheng.teaching.model.dto.WorkDTO;
import com.xuecheng.teaching.model.entity.Work;
import com.xuecheng.teaching.model.vo.IWorkPageList;
import com.xuecheng.teaching.service.WorkService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.stream.Collectors;

@Service
public class WorkServiceImpl implements WorkService {

    @Resource
    private WorkMapper workMapper;

    @Override
    public IWorkPageList queryWorkList(Page<WorkDTO> page, String title) {
        // 构建查询条件
        LambdaQueryWrapper<Work> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(title)) {
            queryWrapper.like(Work::getTitle, title);
        }
        queryWrapper.eq(Work::getStatus, 1);
        queryWrapper.orderByDesc(Work::getCreateDate);

        // 执行分页查询
        Page<Work> workPage = new Page<>(page.getCurrent(), page.getSize());
        workPage = workMapper.selectPage(workPage, queryWrapper);

        // 转换为DTO
        IWorkPageList workPageList = new IWorkPageList();
        workPageList.setItems(workPage.getRecords().stream().map(work -> {
            WorkDTO workDTO = new WorkDTO();
            BeanUtils.copyProperties(work, workDTO);
            workDTO.setBindCourses(workMapper.selectBindCourses(work.getWorkId()));
            workDTO.setUserNum(workMapper.selectUserNum(work.getWorkId()));
            return workDTO;
        }).collect(Collectors.toList()));

        workPageList.setCounts(workPage.getTotal());
        workPageList.setFirst(workPage.getCurrent() == 1);
        workPageList.setItemsSize((int)workPage.getSize());
        workPageList.setPage((int)workPage.getCurrent());
        workPageList.setPages((int)workPage.getPages());
        workPageList.setPageSize((int)workPage.getSize());

        return workPageList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long addWork(WorkDTO workDTO) {
        Work work = new Work();
        BeanUtils.copyProperties(workDTO, work);
        work.setStatus(1);
        work.setCreateDate(LocalDateTime.now());
        work.setChangeDate(LocalDateTime.now());
        workMapper.insert(work);
        return work.getWorkId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateWork(WorkDTO workDTO) {
        Work work = new Work();
        BeanUtils.copyProperties(workDTO, work);
        work.setChangeDate(LocalDateTime.now());

        return workMapper.updateById(work) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWork(Long workId) {

        int i = workMapper.deleteById(workId);
        if (i>0)return true;
        return false;

    }

    @Override
    public WorkDTO getWorkById(Long workId) {
        Work work = workMapper.selectById(workId);
        if (work == null) {
            return null;
        }

        WorkDTO workDTO = new WorkDTO();
        BeanUtils.copyProperties(work, workDTO);
        workDTO.setBindCourses(workMapper.selectBindCourses(workId));
        workDTO.setUserNum(workMapper.selectUserNum(workId));

        return workDTO;
    }
}